// index.js
Page({
  data: {
    currentWord: null,
    showTranslation: false,
    studyProgress: {
      totalWords: 0,
      learnedWords: 0,
      todayLearned: 0,
      streak: 0
    },
    wordList: [],
    currentIndex: 0,
    studyMode: 'learn', // 'learn', 'review', 'test'
    showModeSelector: false
  },

  onLoad() {
    this.loadWordData()
    this.loadStudyProgress()
  },

  onShow() {
    this.loadStudyProgress()
  },

  // 加载单词数据
  loadWordData() {
    const wordData = wx.getStorageSync('wordData') || []
    const unlearnedWords = wordData.filter(word => !word.learned)
    const learnedWords = wordData.filter(word => word.learned)
    
    this.setData({
      wordList: wordData,
      studyProgress: {
        ...this.data.studyProgress,
        totalWords: wordData.length,
        learnedWords: learnedWords.length
      }
    })
    
    if (unlearnedWords.length > 0) {
      this.setData({
        currentWord: unlearnedWords[0],
        currentIndex: wordData.indexOf(unlearnedWords[0])
      })
    } else if (learnedWords.length > 0) {
      this.setData({
        currentWord: learnedWords[0],
        currentIndex: wordData.indexOf(learnedWords[0])
      })
    }
  },

  // 加载学习进度
  loadStudyProgress() {
    const progress = wx.getStorageSync('studyProgress') || {}
    this.setData({
      studyProgress: {
        ...this.data.studyProgress,
        ...progress
      }
    })
  },

  // 切换显示翻译
  toggleTranslation() {
    this.setData({
      showTranslation: !this.data.showTranslation
    })
  },

  // 标记为已学会
  markAsLearned() {
    const { currentWord, currentIndex, wordList } = this.data
    if (!currentWord) return

    const updatedWordList = [...wordList]
    updatedWordList[currentIndex].learned = true
    updatedWordList[currentIndex].reviewCount = 0

    wx.setStorageSync('wordData', updatedWordList)
    
    // 更新学习进度
    this.updateStudyProgress()
    
    // 跳转到下一个单词
    this.nextWord()
  },

  // 标记为需要复习
  markForReview() {
    const { currentWord, currentIndex, wordList } = this.data
    if (!currentWord) return

    const updatedWordList = [...wordList]
    updatedWordList[currentIndex].reviewCount += 1

    wx.setStorageSync('wordData', updatedWordList)
    
    // 跳转到下一个单词
    this.nextWord()
  },

  // 下一个单词
  nextWord() {
    const { wordList, currentIndex, studyMode } = this.data
    let nextIndex = currentIndex + 1

    if (studyMode === 'learn') {
      // 学习模式：找未学会的单词
      const unlearnedWords = wordList.filter(word => !word.learned)
      if (unlearnedWords.length === 0) {
        wx.showToast({
          title: '恭喜！所有单词都学会了！',
          icon: 'success'
        })
        return
      }
      
      const currentUnlearnedIndex = unlearnedWords.findIndex(word => 
        wordList.indexOf(word) === currentIndex
      )
      nextIndex = currentUnlearnedIndex + 1 < unlearnedWords.length 
        ? wordList.indexOf(unlearnedWords[currentUnlearnedIndex + 1])
        : wordList.indexOf(unlearnedWords[0])
    } else if (studyMode === 'review') {
      // 复习模式：找已学会的单词
      const learnedWords = wordList.filter(word => word.learned && word.reviewCount > 0)
      if (learnedWords.length === 0) {
        wx.showToast({
          title: '没有需要复习的单词',
          icon: 'none'
        })
        return
      }
      
      const currentLearnedIndex = learnedWords.findIndex(word => 
        wordList.indexOf(word) === currentIndex
      )
      nextIndex = currentLearnedIndex + 1 < learnedWords.length 
        ? wordList.indexOf(learnedWords[currentLearnedIndex + 1])
        : wordList.indexOf(learnedWords[0])
    }

    this.setData({
      currentIndex: nextIndex,
      currentWord: wordList[nextIndex],
      showTranslation: false
    })
  },

  // 更新学习进度
  updateStudyProgress() {
    const progress = wx.getStorageSync('studyProgress') || {}
    const today = new Date().toDateString()
    
    if (progress.lastStudyDate !== today) {
      progress.todayLearned = 0
      progress.streak += 1
      progress.lastStudyDate = today
    }
    
    progress.todayLearned += 1
    progress.learnedWords += 1
    
    wx.setStorageSync('studyProgress', progress)
    this.loadStudyProgress()
  },

  // 显示模式选择器
  showModeSelector() {
    this.setData({
      showModeSelector: true
    })
  },

  // 隐藏模式选择器
  hideModeSelector() {
    this.setData({
      showModeSelector: false
    })
  },

  // 切换学习模式
  switchMode(e) {
    const mode = e.currentTarget.dataset.mode
    this.setData({
      studyMode: mode,
      showModeSelector: false,
      showTranslation: false
    })
    
    // 根据模式重新选择单词
    this.selectWordByMode(mode)
  },

  // 根据模式选择单词
  selectWordByMode(mode) {
    const { wordList } = this.data
    let targetWords = []
    
    if (mode === 'learn') {
      targetWords = wordList.filter(word => !word.learned)
    } else if (mode === 'review') {
      targetWords = wordList.filter(word => word.learned && word.reviewCount > 0)
    } else if (mode === 'test') {
      targetWords = wordList.filter(word => word.learned)
    }
    
    if (targetWords.length > 0) {
      const firstWord = targetWords[0]
      this.setData({
        currentWord: firstWord,
        currentIndex: wordList.indexOf(firstWord)
      })
    }
  },

  // 跳转到统计页面
  goToStats() {
    wx.navigateTo({
      url: '/pages/stats/stats'
    })
  },

  // 跳转到设置页面
  goToSettings() {
    wx.navigateTo({
      url: '/pages/settings/settings'
    })
  }
})
